ActionScript 3.0 Cookbook by Joey Lott & Darron Schall & Keith Peters
Author:Joey Lott & Darron Schall & Keith Peters [Joey Lott]
Language: eng
Format: epub
Tags: COMPUTERS / Programming / Object Oriented
ISBN: 9780596158378
Publisher: Adobe Dev Library
Published: 2008-12-16T16:00:00+00:00
See Also
Recipe 11.1
Easing
Problem
You want an object to smoothly move to a specific location, slow down, and stop as it reaches that spot.
Solution
Use an easing formula.
Discussion
First, we’ll look at the concept of simple easing. You have an object at a certain position and you want it to ease to another position. Take the distance between the two points and move the object a fraction of that distance—maybe one-half, one-third, or less. On the next iteration, find the new distance and move the object a fraction of that. Continue this way until the object is so close to the target that you can consider it there.
You’ll see that the first couple of jumps are quite big, but successive jumps get smaller and smaller until the object appears not to be moving at all. Viewed in terms of velocity, the velocity starts out high and approaches zero. Another way of looking at it is that velocity is dependent on distance. A large distance makes for a high velocity.
The following example shows a simple example of easing. The target position is specified by _targetX and _targetY. The fraction that the object moves each time is set in _easingSpeed. Here it is set to 0.1, which means the object moves one-tenth of the distance to the target on each animation interval:
package { import flash.display.Sprite; import flash.events.Event; import flash.events.TimerEvent; import flash.utils.Timer; public class Easing extends Sprite { private var _sprite:Sprite; private var _easingSpeed:Number = 0.1; private var _targetX:Number = 400; private var _targetY:Number = 200; private var _timer:Timer; public function Easing() { _sprite = new Sprite(); _sprite.graphics.beginFill(0x0000ff, 100); _sprite.graphics.drawCircle(0, 0, 25); _sprite.graphics.endFill(); _sprite.x = 50; _sprite.y = 50; addChild(_sprite); _timer = new Timer(30); _timer.addEventListener("timer", onTimer); _timer.start(); } public function onTimer(event:TimerEvent):void { var vx:Number = (_targetX - _sprite.x) * _easingSpeed; var vy:Number = (_targetY - _sprite.y) * _easingSpeed; _sprite.x += vx; _sprite.y += vy; } } }
One problem with this setup is that the timer continues to run, even after the object has gotten as close as it’s going to get to the target. To handle that, find the distance to the target and if it is less than a certain value, just turn off the timer, as illustrated by the bolded code in the following example:
public function onTimer(event:TimerEvent):void { var dx:Number = _targetX - _sprite.x; var dy:Number = _targetY - _sprite.y; var dist:Number = Math.sqrt(dx * dx + dy * dy); if(dist < 1) { _sprite.x = _targetX; _sprite.y = _targetY; _timer.stop(); } else { var vx:Number = dx * _easingSpeed; var vy:Number = dy * _easingSpeed; _sprite.x += vx; _sprite.y += vy; } }
This example first finds the distance on the two axes and the total distance. If the distance is less than 1, it places the object at the target point and kills the timer. Otherwise, it continues as normal.
Sometimes though, you may not want the easing to stop; for example, in a moving target. The following example has the object easing toward the mouse. In other words, it simply replaces mouseX and mouseY
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Hello! Python by Anthony Briggs(9914)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9337)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7778)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Svelte with Test-Driven Development by Daniel Irvine(7143)
Test-Driven Development with PHP 8 by Rainier Sarabia(6871)
Layered Design for Ruby on Rails Applications by Dementyev Vladimir;(6740)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(6532)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Web Development with Django by Ben Shaw Saurabh Badhwar(6208)
React Application Architecture for Production by Alan Alickovic(5932)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(5806)
Kotlin in Action by Dmitry Jemerov(5062)
Audition by Ryu Murakami(4583)
Software Architecture for Web Developers by Mihaela Roxana Ghidersa(4444)
Hands-On Full-Stack Web Development with GraphQL and React by Sebastian Grebe(4316)
Accelerating Server-Side Development with Fastify by Manuel Spigolon Maksim Sinik & Matteo Collina(4293)
Functional Programming in JavaScript by Mantyla Dan(4038)
